import { reactive, ref } from "vue";
import { getListApi } from "@/api/configCenter/countryRegion";
import { getCoumsterChannelByCountryApi } from "@/api/common";
import CommonHook from "@/hook/commonHook";
export default function InitHook(state: any) {
  const { passData } = state;
  const formDataRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const { getClassifyArrayByFields } = CommonHook();
  const formState = reactive<Partial<Base.GeneraDataType>>({
    countryRegionId: undefined,
    channelId: undefined
  });
  const channelOpts = ref<Array<Base.GeneraDataType>>([]);
  const countryOpts = ref<Array<Base.GeneraDataType>>([]);
  const getAllCountryOpts = () => {
    return getListApi({ pageNum: 1, pageSize: 999999, status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        countryOpts.value = data.records || [];
      }
    });
  };
  const getChannelOpts = () => {
    return getCoumsterChannelByCountryApi({
      countryRegionId: formState.countryRegionId,
      warehouseCode: passData.value.warehouseCode,
      customerId: passData.value.customerId
    }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        channelOpts.value = getClassifyArrayByFields(data || [], "channelAllocation", "disable");
      }
    });
  };
  return {
    formState,
    loading,
    formDataRef,
    countryOpts,
    getAllCountryOpts,
    channelOpts,
    getChannelOpts
  };
}
